package com.flyco.modules.stm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.common.api.vo.Result;
import com.flyco.common.util.BizUtil;
import com.flyco.common.util.HttpUtil;
import com.flyco.common.util.MD5Util;
import com.flyco.modules.stm.entity.*;
import com.flyco.modules.stm.mapper.OaWldbMapper;
import com.flyco.modules.stm.model.ReplenishmentContext;
import com.flyco.modules.stm.model.RetreatContext;
import com.flyco.modules.stm.service.IOaWldbService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: oa要货退仓表
 * @Author: flyco
 * @Date: 2022-12-12
 * @Version: V1.0
 */
@DS("oa")
@Service
public class OaWldbServiceImpl extends ServiceImpl<OaWldbMapper, OaWldb> implements IOaWldbService {
    @Value("${flyco.oa.secret}")
    private String   secret;

    /**
     * 写到171的"FLYCO_MD"."OA_WLDB"表，V_ZTB_LGORT视图维护了SAP门店信息
     */
    @Transactional
    @Override
    public Result<?> pushRetreatOA(OaPushDTO oaPushDTO, RetreatContext retreatContext, String userName) {
        String dateTime = DateUtil.formatDateTime(new Date());
        StmRetreat stmRetreat = retreatContext.getStmRetreat();

        //组装oa数据
        List<OaWldb> oaWldbList = new ArrayList<>();
        retreatContext.getItemList().forEach(item -> {
            OaWldb oaWldb = new OaWldb();
            oaWldb.setSqr(userName);
            oaWldb.setGs("1000");
            oaWldb.setStatus("1");
            oaWldb.setDbck("0");
            oaWldb.setBm(stmRetreat.getStoreName());
            oaWldb.setXqsl(item.getProductQty());
            oaWldb.setCpbm("1000" + retreatContext.getSapItemCodeMap().get(item.getProductId()).get(0).getMaterialCode());
            oaWldb.setDcgc("1010");
            oaWldb.setDckcdd(stmRetreat.getSapAddressCode());
            oaWldb.setDrgc(oaPushDTO.getInOutCode());
            oaWldb.setDrkcdd(oaPushDTO.getInOutAddressCode());
            oaWldb.setDbdh("退仓" + stmRetreat.getId());
            oaWldb.setDatewritten(dateTime);
            oaWldb.setTimewritten(dateTime);
            oaWldb.setZy(stmRetreat.getStoreName() + dateTime + "退仓");
            oaWldbList.add(oaWldb);
        });
        OaWldbDTO oaWldbDTO = new OaWldbDTO();
        oaWldbDTO.setTimestamp(new Date().getTime());
        String sign= MD5Util.MD5Encode(secret + oaWldbDTO.getTimestamp(), null);
        oaWldbDTO.setSing(sign);
        oaWldbDTO.setOaWldbList(oaWldbList);
        String result = HttpUtil.postJsonRequest(BizUtil.getOaUrl() + "/oa/save", JSON.toJSONString(oaWldbDTO));
        return Result.ok(result);
    }

    /**
     * 写到171的"FLYCO_MD"."OA_WLDB"表，V_ZTB_LGORT视图维护了SAP门店信息
     */
    @Transactional
    @Override
    public Result<?> pushReplenishmentOA(OaPushDTO oaPushDTO, ReplenishmentContext replenishmentContext, String userName) {
        String dateTime = DateUtil.formatDateTime(new Date());
        StmReplenishment stmReplenishment = replenishmentContext.getStmReplenishment();

        //组装oa数据
        List<OaWldb> oaWldbList = new ArrayList<>();
        replenishmentContext.getItemList().forEach(item -> {
            OaWldb oaWldb = new OaWldb();
            oaWldb.setSqr(userName);
            oaWldb.setGs("1000");
            oaWldb.setStatus("1");
            oaWldb.setDbck("0");
            oaWldb.setBm(stmReplenishment.getStoreName());
            oaWldb.setXqsl(item.getProductQty());
            oaWldb.setCpbm("1000" + replenishmentContext.getSapItemCodeMap().get(item.getProductId()).get(0).getMaterialCode());
            oaWldb.setDcgc(oaPushDTO.getInOutCode());
            oaWldb.setDckcdd(oaPushDTO.getInOutAddressCode());
            oaWldb.setDrgc("1010");
            oaWldb.setDrkcdd(stmReplenishment.getSapAddressCode());
            oaWldb.setDbdh("要货" + stmReplenishment.getId());
            oaWldb.setDatewritten(dateTime);
            oaWldb.setTimewritten(dateTime);
            oaWldb.setZy(stmReplenishment.getStoreName() + dateTime + "要货");
            oaWldbList.add(oaWldb);
        });
        OaWldbDTO oaWldbDTO = new OaWldbDTO();
        oaWldbDTO.setTimestamp(new Date().getTime());
        String sign= MD5Util.MD5Encode(secret + oaWldbDTO.getTimestamp(), null);
        oaWldbDTO.setSing(sign);
        oaWldbDTO.setOaWldbList(oaWldbList);
        String result = HttpUtil.postJsonRequest(BizUtil.getOaUrl() + "/oa/save", JSON.toJSONString(oaWldbDTO));
        return Result.ok(result);
    }
}
